/**
 * 
 */
package co.edu.udem.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import co.edu.udem.dao.SubjectDetailDao;
import co.edu.udem.domain.SubjectDetail;
import co.edu.udem.service.login.SessionManager;

/**
 * @since 29/07/2012
 */
@Repository("subjectDetailDao")
@Transactional
public class SubjectDetailDaoImpl implements SubjectDetailDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/** Instance of session. */
	@Autowired
	private SessionManager sessionManager;

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.edu.udem.dao.SubjectDao#getObjectList(java.util.Date,
	 * java.lang.String[][], java.lang.Object[])
	 */
	public List<SubjectDetail> getObjectList(Date serviceDate,
			String[][] orderBy, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	public void addSubjectDetail(SubjectDetail subjectDetail) {
		Session session = sessionFactory.getCurrentSession();
		session.merge(subjectDetail);
	}

	public List<SubjectDetail> getSubjectDetailListBySubject(String subjectCode) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("FROM  SubjectDetail det Where subjectCode = :subjectCode ");
		query.setParameter("subjectCode", subjectCode);
		return (List<SubjectDetail>) query.list();
	}

	public List<SubjectDetail> getSubjectDetailListByUserSubject(
			String userCode, int userType) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("FROM  SubjectDetail Where subjectCode IN (Select Distinct subjectCode From UserSubject Where userCode = :userCode And userType = :userType)");
		query.setParameter("userCode", userCode);
		query.setParameter("userType", userType);
		return (List<SubjectDetail>) query.list();
	}
	
	public SubjectDetail getSubjectDetailById(String subjectDetailCode) {
		Session session = sessionFactory.getCurrentSession();
		SubjectDetail subject = (SubjectDetail) session.get(SubjectDetail.class, subjectDetailCode);
		return subject;
	}
	
	public void delete(String subjectDetailCode) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("DELETE FROM SubjectDetail Where subjectDetailCode = :subjectDetailCode");
		query.setParameter("subjectDetailCode", subjectDetailCode);
		query.executeUpdate();
	}
}
